package com.sky.com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.ThreadLocalUtil;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    //条件查询
    @Override
    public List<AddressBook> getList() {
        AddressBook addressBook = AddressBook.builder()
                .userId(ThreadLocalUtil.getCurrentId())
                .build();
        return addressBookMapper.getList(addressBook);
    }

    /**
     * 查询当前用户的收货地址
     **/


    @Override
    public void save(AddressBook addressBook) {
        addressBook.setUserId(ThreadLocalUtil.getCurrentId()); //用户id
        addressBook.setIsDefault(0);//非默认地址
        addressBookMapper.insert(addressBook);
    }

    @Override
    public AddressBook getDefault() {
        AddressBook addressBook = AddressBook
                .builder()
                .userId(ThreadLocalUtil.getCurrentId())
                .isDefault(1)
                .build();
        List<AddressBook> list = addressBookMapper.getList(addressBook);
        return list.get(0);
    }


    /**
     * 根据ID查询收货地址
     *
     * @param id
     * @return
     **/
    @Override
    public AddressBook getById(Long id) {
        return addressBookMapper.getById(id);
    }


    /**
     * 修改收货地址
     **/
    @Override
    public void updateById(AddressBook addressBook) {
        addressBookMapper.updateById(addressBook);
    }

    //删除地址
    @Override
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }


    /**
     * 新增收货地址
     *
     * @param addressBook
     **/
    @Override
    public void add(AddressBook addressBook) {
        //1.补全数据
        //uid
        addressBook.setUserId(ThreadLocalUtil.getCurrentId());
        //保存数据
        addressBookMapper.insert(addressBook);
    }

    /**
     * 设置默认收货地址
     *
     * @param addressBook
     **/
    @Override
    @Transactional
    public void setDefaultAddress(AddressBook addressBook) {
       //1. 将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id ?
        addressBook.setIsDefault(0);
        addressBook.setUserId(ThreadLocalUtil.getCurrentId());
        addressBookMapper.updateIsDefaultByUserId(addressBook);

        //2.将当前地址改为默认地址 update address_book set is_default = ? where id = ?
        addressBook.setIsDefault(1);
        addressBookMapper.updateById(addressBook);
    }

    /**
     * 查询默认收货地址
     *
     * @return
     **/
    @Override
    public AddressBook getDefaultAddress() {
        AddressBook addressBook = AddressBook.builder()
                .userId(ThreadLocalUtil.getCurrentId()) //登录用户id
                .isDefault(1) //默认选中状态
                .build();
        List<AddressBook> list = addressBookMapper.getList(addressBook);
        return list.get(0);
    }
}
